<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>收货地址管理 - 钓鱼小程序</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        /* 移动端滚动条优化 - 隐藏滚动条 */
        ::-webkit-scrollbar {
            display: none;
        }
        
        * {
            -ms-overflow-style: none;
            scrollbar-width: none;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f5f7fa;
            padding-bottom: 80px;
            padding-top: 50px;
            color: #333;
            line-height: 1.6;
            max-width: 375px;
            margin: 0 auto;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .header {
            position: fixed;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 100%;
            max-width: 375px;
            background: #3498DB;
            color: white;
            padding: 12px 16px;
            display: flex;
            align-items: center;
            gap: 12px;
            z-index: 100;
            justify-content: space-between;
        }

        .back-btn {
            background: none;
            border: none;
            color: white;
            font-size: 18px;
            cursor: pointer;
        }

        .header h1 {
            font-size: 18px;
            font-weight: 600;
            flex: 1;
            text-align: center;
        }
        
        /* 小程序胶囊按钮 */
        .capsule-btn {
            background: rgba(255,255,255,0.2);
            border-radius: 16px;
            padding: 6px 12px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .capsule-btn .dot {
            width: 4px;
            height: 4px;
            background: white;
            border-radius: 50%;
        }

        .main-content {
            flex: 1;
            padding: 16px;
        }

        .address-list {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .address-card {
            background: white;
            border-radius: 12px;
            padding: 16px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            position: relative;
        }

        .address-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 8px;
        }

        .address-contact {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .contact-name {
            font-size: 16px;
            font-weight: 600;
            color: #333;
        }

        .contact-phone {
            font-size: 14px;
            color: #666;
        }

        .default-badge {
            background: #3498DB;
            color: white;
            font-size: 10px;
            padding: 2px 6px;
            border-radius: 8px;
            margin-left: 8px;
        }

        .address-actions {
            display: flex;
            gap: 8px;
        }

        .action-btn {
            background: none;
            border: none;
            color: #666;
            font-size: 14px;
            cursor: pointer;
            padding: 4px 8px;
            border-radius: 4px;
            transition: all 0.2s;
        }

        .action-btn:hover {
            background: #f0f0f0;
        }

        .edit-btn {
            color: #3498DB;
        }

        .delete-btn {
            color: #e74c3c;
        }

        .address-detail {
            font-size: 14px;
            color: #666;
            line-height: 1.5;
            margin-bottom: 12px;
        }

        .address-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .address-tags {
            display: flex;
            gap: 6px;
        }

        .address-tag {
            background: #f8f9fa;
            color: #666;
            font-size: 12px;
            padding: 2px 8px;
            border-radius: 10px;
            border: 1px solid #e9ecef;
        }

        .set-default-btn {
            background: none;
            border: 1px solid #3498DB;
            color: #3498DB;
            font-size: 12px;
            padding: 4px 12px;
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.2s;
        }

        .set-default-btn:hover {
            background: #3498DB;
            color: white;
        }

        .set-default-btn.is-default {
            background: #3498DB;
            color: white;
            cursor: default;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #999;
        }

        .empty-icon {
            font-size: 48px;
            margin-bottom: 16px;
        }

        .empty-text {
            font-size: 16px;
            margin-bottom: 8px;
        }

        .empty-desc {
            font-size: 14px;
            color: #bbb;
        }

        .add-address-btn {
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            width: calc(100% - 32px);
            max-width: 343px;
            background: #3498DB;
            color: white;
            border: none;
            border-radius: 25px;
            padding: 16px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            box-shadow: 0 4px 12px rgba(52,152,219,0.3);
            transition: all 0.3s;
        }

        .add-address-btn:hover {
            transform: translateX(-50%) translateY(-2px);
            box-shadow: 0 6px 20px rgba(52,152,219,0.4);
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 1000;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .modal.active {
            display: flex;
        }

        .modal-content {
            background: white;
            border-radius: 12px;
            width: 100%;
            max-width: 320px;
            max-height: 80vh;
            overflow-y: auto;
        }

        .modal-header {
            padding: 20px 20px 0;
            text-align: center;
            border-bottom: 1px solid #f0f0f0;
            margin-bottom: 20px;
        }

        .modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
        }

        .modal-body {
            padding: 0 20px 20px;
        }

        .form-group {
            margin-bottom: 16px;
        }

        .form-label {
            font-size: 14px;
            color: #333;
            margin-bottom: 8px;
            display: block;
            font-weight: 500;
        }

        .form-input, .form-select, .form-textarea {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.2s;
        }

        .form-input:focus, .form-select:focus, .form-textarea:focus {
            outline: none;
            border-color: #3498DB;
        }

        .form-textarea {
            min-height: 80px;
            resize: vertical;
        }

        .form-row {
            display: flex;
            gap: 12px;
        }

        .form-row .form-group {
            flex: 1;
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-top: 16px;
        }

        .checkbox {
            width: 18px;
            height: 18px;
            border: 2px solid #ddd;
            border-radius: 3px;
            position: relative;
            cursor: pointer;
            transition: all 0.2s;
        }

        .checkbox.checked {
            background: #3498DB;
            border-color: #3498DB;
        }

        .checkbox.checked::after {
            content: '✓';
            position: absolute;
            top: -2px;
            left: 2px;
            color: white;
            font-size: 12px;
            font-weight: bold;
        }

        .checkbox-label {
            font-size: 14px;
            color: #333;
            cursor: pointer;
        }

        .modal-footer {
            padding: 20px;
            border-top: 1px solid #f0f0f0;
            display: flex;
            gap: 12px;
        }

        .modal-btn {
            flex: 1;
            padding: 12px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
        }

        .cancel-btn {
            background: #f8f9fa;
            color: #666;
            border: 1px solid #e9ecef;
        }

        .cancel-btn:hover {
            background: #e9ecef;
        }

        .confirm-btn {
            background: #3498DB;
            color: white;
        }

        .confirm-btn:hover {
            background: #2980B9;
        }

        .confirm-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
        }

        /* 确认删除模态框 */
        .confirm-modal .modal-content {
            max-width: 280px;
        }

        .confirm-modal .modal-body {
            text-align: center;
            padding: 20px;
        }

        .confirm-icon {
            font-size: 48px;
            color: #e74c3c;
            margin-bottom: 16px;
        }

        .confirm-text {
            font-size: 16px;
            color: #333;
            margin-bottom: 8px;
        }

        .confirm-desc {
            font-size: 14px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="header">
        <button class="back-btn" onclick="goBack()">←</button>
        <h1>收货地址</h1>
        <div class="capsule-btn">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
    </div>

    <div class="main-content">
        <div class="address-list" id="addressList">
            <!-- 地址列表将通过JavaScript动态生成 -->
        </div>
    </div>

    <button class="add-address-btn" onclick="showAddAddressModal()">
        + 新增收货地址
    </button>

    <!-- 新增/编辑地址模态框 -->
    <div class="modal" id="addressModal">
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-title" id="modalTitle">新增收货地址</div>
            </div>
            <div class="modal-body">
                <form id="addressForm">
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">收货人</label>
                            <input type="text" class="form-input" id="recipientName" placeholder="请输入收货人姓名" required>
                        </div>
                        <div class="form-group">
                            <label class="form-label">手机号码</label>
                            <input type="tel" class="form-input" id="recipientPhone" placeholder="请输入手机号码" required>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">所在地区</label>
                        <select class="form-select" id="region" required>
                            <option value="">请选择省市区</option>
                            <option value="北京市-北京市-朝阳区">北京市 朝阳区</option>
                            <option value="北京市-北京市-海淀区">北京市 海淀区</option>
                            <option value="上海市-上海市-浦东新区">上海市 浦东新区</option>
                            <option value="上海市-上海市-黄浦区">上海市 黄浦区</option>
                            <option value="广东省-广州市-天河区">广东省 广州市 天河区</option>
                            <option value="广东省-深圳市-南山区">广东省 深圳市 南山区</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">详细地址</label>
                        <textarea class="form-textarea" id="detailAddress" placeholder="请输入详细地址，如街道、门牌号、小区、楼栋号等" required></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">地址标签</label>
                        <select class="form-select" id="addressTag">
                            <option value="家">家</option>
                            <option value="公司">公司</option>
                            <option value="学校">学校</option>
                            <option value="其他">其他</option>
                        </select>
                    </div>
                    
                    <div class="checkbox-group">
                        <div class="checkbox" id="defaultCheckbox" onclick="toggleDefault()"></div>
                        <label class="checkbox-label" onclick="toggleDefault()">设为默认地址</label>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="modal-btn cancel-btn" onclick="hideAddressModal()">取消</button>
                <button class="modal-btn confirm-btn" onclick="saveAddress()">保存</button>
            </div>
        </div>
    </div>

    <!-- 确认删除模态框 -->
    <div class="modal confirm-modal" id="confirmModal">
        <div class="modal-content">
            <div class="modal-body">
                <div class="confirm-icon">🗑️</div>
                <div class="confirm-text">确认删除地址？</div>
                <div class="confirm-desc">删除后无法恢复</div>
            </div>
            <div class="modal-footer">
                <button class="modal-btn cancel-btn" onclick="hideConfirmModal()">取消</button>
                <button class="modal-btn confirm-btn" onclick="confirmDelete()" style="background: #e74c3c;">删除</button>
            </div>
        </div>
    </div>

    <script>
        // 模拟地址数据
        let addresses = [
            {
                id: 1,
                name: '张三',
                phone: '138****8888',
                region: '北京市-北京市-朝阳区',
                detail: '三里屯街道工体北路8号院1号楼101室',
                tag: '家',
                isDefault: true
            },
            {
                id: 2,
                name: '李四',
                phone: '139****9999',
                region: '上海市-上海市-浦东新区',
                detail: '陆家嘴金融贸易区世纪大道88号金茂大厦2层',
                tag: '公司',
                isDefault: false
            }
        ];

        let editingAddressId = null;
        let deletingAddressId = null;

        // 渲染地址列表
        function renderAddressList() {
            const addressList = document.getElementById('addressList');
            
            if (addresses.length === 0) {
                addressList.innerHTML = `
                    <div class="empty-state">
                        <div class="empty-icon">📍</div>
                        <div class="empty-text">暂无收货地址</div>
                        <div class="empty-desc">点击下方按钮添加收货地址</div>
                    </div>
                `;
                return;
            }

            addressList.innerHTML = addresses.map(address => `
                <div class="address-card">
                    <div class="address-header">
                        <div class="address-contact">
                            <span class="contact-name">${address.name}</span>
                            <span class="contact-phone">${address.phone}</span>
                            ${address.isDefault ? '<span class="default-badge">默认</span>' : ''}
                        </div>
                        <div class="address-actions">
                            <button class="action-btn edit-btn" onclick="editAddress(${address.id})">编辑</button>
                            <button class="action-btn delete-btn" onclick="showDeleteConfirm(${address.id})">删除</button>
                        </div>
                    </div>
                    <div class="address-detail">
                        ${address.region.replace(/-/g, ' ')} ${address.detail}
                    </div>
                    <div class="address-footer">
                        <div class="address-tags">
                            <span class="address-tag">${address.tag}</span>
                        </div>
                        ${!address.isDefault ? `<button class="set-default-btn" onclick="setDefault(${address.id})">设为默认</button>` : '<button class="set-default-btn is-default">默认地址</button>'}
                    </div>
                </div>
            `).join('');
        }

        // 显示新增地址模态框
        function showAddAddressModal() {
            editingAddressId = null;
            document.getElementById('modalTitle').textContent = '新增收货地址';
            document.getElementById('addressForm').reset();
            document.getElementById('defaultCheckbox').classList.remove('checked');
            document.getElementById('addressModal').classList.add('active');
        }

        // 隐藏地址模态框
        function hideAddressModal() {
            document.getElementById('addressModal').classList.remove('active');
        }

        // 编辑地址
        function editAddress(id) {
            const address = addresses.find(addr => addr.id === id);
            if (!address) return;

            editingAddressId = id;
            document.getElementById('modalTitle').textContent = '编辑收货地址';
            document.getElementById('recipientName').value = address.name;
            document.getElementById('recipientPhone').value = address.phone.replace(/\*/g, '');
            document.getElementById('region').value = address.region;
            document.getElementById('detailAddress').value = address.detail;
            document.getElementById('addressTag').value = address.tag;
            
            if (address.isDefault) {
                document.getElementById('defaultCheckbox').classList.add('checked');
            } else {
                document.getElementById('defaultCheckbox').classList.remove('checked');
            }
            
            document.getElementById('addressModal').classList.add('active');
        }

        // 保存地址
        function saveAddress() {
            const form = document.getElementById('addressForm');
            if (!form.checkValidity()) {
                form.reportValidity();
                return;
            }

            const name = document.getElementById('recipientName').value;
            const phone = document.getElementById('recipientPhone').value;
            const region = document.getElementById('region').value;
            const detail = document.getElementById('detailAddress').value;
            const tag = document.getElementById('addressTag').value;
            const isDefault = document.getElementById('defaultCheckbox').classList.contains('checked');

            // 手机号格式验证
            if (!/^1[3-9]\d{9}$/.test(phone)) {
                alert('请输入正确的手机号码');
                return;
            }

            if (editingAddressId) {
                // 编辑现有地址
                const addressIndex = addresses.findIndex(addr => addr.id === editingAddressId);
                if (addressIndex !== -1) {
                    addresses[addressIndex] = {
                        ...addresses[addressIndex],
                        name,
                        phone: phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'),
                        region,
                        detail,
                        tag,
                        isDefault
                    };
                }
            } else {
                // 新增地址
                const newAddress = {
                    id: Date.now(),
                    name,
                    phone: phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'),
                    region,
                    detail,
                    tag,
                    isDefault
                };
                addresses.push(newAddress);
            }

            // 如果设置为默认地址，取消其他地址的默认状态
            if (isDefault) {
                addresses.forEach(addr => {
                    if (addr.id !== editingAddressId) {
                        addr.isDefault = false;
                    }
                });
            }

            renderAddressList();
            hideAddressModal();
        }

        // 设置默认地址
        function setDefault(id) {
            addresses.forEach(addr => {
                addr.isDefault = addr.id === id;
            });
            renderAddressList();
        }

        // 显示删除确认
        function showDeleteConfirm(id) {
            deletingAddressId = id;
            document.getElementById('confirmModal').classList.add('active');
        }

        // 隐藏删除确认
        function hideConfirmModal() {
            document.getElementById('confirmModal').classList.remove('active');
            deletingAddressId = null;
        }

        // 确认删除
        function confirmDelete() {
            if (deletingAddressId) {
                addresses = addresses.filter(addr => addr.id !== deletingAddressId);
                renderAddressList();
                hideConfirmModal();
            }
        }

        // 切换默认地址复选框
        function toggleDefault() {
            const checkbox = document.getElementById('defaultCheckbox');
            checkbox.classList.toggle('checked');
        }

        // 返回上一页
        function goBack() {
            // 这里可以实现返回逻辑
            history.back();
        }

        // 初始化页面
        renderAddressList();
    </script>
</body>
</html> 